package hibernate1;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HibernateTest1 {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Test
	public void testComponent() {
		Worker worker = new Worker();
		Pay pay = new Pay();
		pay.setMounthlyPay(1000);
		pay.setYearPay(80000);
		pay.setVocationWithPay(5);
		worker.setName("ABCD");
		worker.setPay(pay);
		
		session.save(worker);
	}
	
	@Before
	public void init(){
		System.out.println("InitMethodInvoke");
		Configuration configuration = new Configuration().configure();
		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	
	@After
	public void destroy(){
		System.out.println("DestroyMethodInvoke");
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}
